package com.yonyou.crm.base.product.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.base.measure.service.MeasuresBizServiceImpl;
import com.yonyou.crm.base.product.entity.ProductVO;
import com.yonyou.crm.base.product.entity.SaleUnitVO;
import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.enumeration.service.EnumBizServiceImpl;
import com.yonyou.crm.common.field.handler.FieldRelationHandler;
import com.yonyou.crm.common.field.relation.EnumFieldRelation;
import com.yonyou.crm.common.field.relation.FieldRelation;
import com.yonyou.crm.common.field.relation.RefFieldRelation;

@Service
public class SaleUnitBizServiceImpl extends BaseBizServiceImpl<SaleUnitVO> {

	@Autowired
	private EnumBizServiceImpl enumService;
	@Autowired
	private MeasuresBizServiceImpl measuresService;
	
	/**
	 * 根据产品id查询销售单位
	 * @param productId
	 * @return
	 */
	public List<SaleUnitVO> getSUListByPrdId(Long productId) {
		List<SaleUnitVO> voList = dao.selectVoList(SaleUnitVO.class, "getSUListByPrdId", productId);
		trans(voList);
		return voList;
	}
	
	/**
	 * 销售单位字段翻译
	 * @param ids
	 * @return
	 */
	public Object selectSaleUnitById(Object[] ids) {
		Map<String, Object> para = new HashMap<String, Object>();
		para.put("ids", ids);
		return dao.selectList(SaleUnitVO.class, "selectSaleUnitById", para);
	}
	
	/**
	 * 删除销售单位
	 * @param code
	 */
	public void deleteSaleUnit(Long id) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("id", id);
		dao.deleteSql(SaleUnitVO.class, "deleteSaleUnit", paramMap);
	}
	
	/**
	 * 新增销售单位
	 * @param vo
	 */
	public void insertSaleUnit(ProductVO vo) {
		
		List<SaleUnitVO> voList = fillSaleUnit(vo);
		if (voList != null && voList.size() > 0) {
			insertSaleUnit(voList);
		}
	}
	
	/**
	 * 修改销售单位
	 * @param vo
	 */
	public void updateSaleUnit(ProductVO vo) {
		
		List<SaleUnitVO> voList = fillSaleUnit(vo);
		if (voList != null && voList.size() > 0) {
			deleteSaleUnit(vo.getId());
			insertSaleUnit(voList);
		}
	}
	
	/**
	 * 批量删除销售单位
	 * @param codes
	 */
	public void batchDeleteSaleUnit(String[] ids) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("ids", ids);
		dao.deleteSql(SaleUnitVO.class, "batchDeleteSaleUnit", paramMap);
	}
	
	/**
	 * 销售单位列表
	 * @param vo
	 * @return
	 */
	private List<SaleUnitVO> fillSaleUnit(ProductVO vo) {
		
		if (vo == null || vo.getSaleUnits() == null || vo.getSaleUnits().size() == 0) {
			return null;
		}
		
		List<SaleUnitVO> voList = new ArrayList<SaleUnitVO>();
		for (SaleUnitVO suVO : vo.getSaleUnits()) {
			suVO.setProductId(vo.getId());
			voList.add(suVO);
		}
		
		return voList;
	}
	
	private void insertSaleUnit(List<SaleUnitVO> voList) {
		dao.saveSql(SaleUnitVO.class, "insertSaleUnit", voList);
	}
	
	private List<SaleUnitVO> trans(List<SaleUnitVO> voList){
		if(voList != null && voList.size() > 0) {
			List<FieldRelation<SaleUnitVO>> relations = new ArrayList<FieldRelation<SaleUnitVO>>();
			relations.add(new EnumFieldRelation<SaleUnitVO>(SaleUnitVO.class, "fixedConvert", "fixedConvertName", enumService, "whether"));
			relations.add(new RefFieldRelation<SaleUnitVO>(SaleUnitVO.class, "measureId", new String[]{"measureName"}, measuresService, "selectFieldsByIds"));
			new FieldRelationHandler<SaleUnitVO>().handler(voList, relations);
		}
		return voList;
	}
}
